package Service.Impl;

import Dao.Impl.UserDaoImpl;
import Dao.UserDao;
import Domain.PageBean;
import Domain.User;
import Service.UserService;

import java.util.List;
import java.util.Map;

public class UserServiceImpl implements UserService {
    private UserDao dao = new UserDaoImpl();
    @Override
    public PageBean<User> findUserByPage(String _currentPage, String _rows, Map<String, String[]> condition) {
        int currentPage = Integer.parseInt(_currentPage);
        int rows = Integer.parseInt(_rows);

        if(currentPage<=0){
            currentPage=1;
        }

        PageBean<User> pb = new PageBean<>();

        pb.setRows(rows);

        int totalCount = dao.findTotalCount(condition);
        pb.setTotalCount(totalCount);
        int start=(currentPage -1) * rows;
        List<User> list = dao.findByPage(start, rows, condition);
        pb.setList(list);

        int totalPage = (totalCount % rows) == 0 ? totalCount/rows : (totalCount/rows)+1;
        pb.setTotalPage(totalPage);


        if(currentPage>=totalPage){
            currentPage=totalPage;
        }
        pb.setCurrentPage(currentPage);
        return pb;
    }

    @Override
    public List<User> findUserById(int id) {
        return dao.findUserById(id);
    }

    @Override
    public void updateUser(User user) {
        dao.updateUser(user);
    }

    @Override
    public void deleteUser(int id) {
        dao.deleteUser(id);
    }

    @Override
    public void delSelected(String[] ids) {
        if(ids!=null && ids.length>0){
            for (String id : ids) {
                dao.deleteUser(Integer.parseInt(id));
            }
        }
    }

    @Override
    public List<User> UserLogin(User user) {
        return dao.UserLogin(user);
    }

    @Override
    public void AddUser(User user) {
        dao.AddUser(user);
    }

    @Override
    public List<User> FindUserByUsername(String username) {
        return dao.FindUserByUsername(username);
    }

    @Override
    public void changePassword(int id, String password) {
        dao.changePassword(id,password);
    }

    @Override
    public void SetSecurity(int id, String question, String answer) {
        dao.SetSecurity(id,question,answer);
    }

    @Override
    public void UpdateUserInf(int id, String username, String header, String tel, String address) {
        dao.UpdateUserInf(id,username,header,tel,address);
    }


}
